import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tiktokadult/assets_tool/images.dart';
import 'package:tiktokadult/hj_utils/widget_utils.dart';

import '../../../hj_model/home/module_detail_model.dart';
import '../../../hj_model/home/video_list_resp.dart';
import '../../../hj_utils/api_service/vid_service.dart';
import '../../../tools_base/debug_log.dart';
import '../special_topic_detail/specail_topics_detail_page.dart';

class SectionExchangeWidget extends StatefulWidget {
  final AllSection? item;
  final bool isDarkStyle;
  final Function? callback;
  final EdgeInsetsGeometry? margin;

  SectionExchangeWidget({
    required this.item,
    this.isDarkStyle = false,
    this.callback,
    this.margin,
  });

  @override
  State<StatefulWidget> createState() {
    return _SectionExchangeWidgetState();
  }
}

class _SectionExchangeWidgetState extends State<SectionExchangeWidget> {
  bool isLoading = false;

  void _loadData() async {
    try {
      if (isLoading) return;
      isLoading = true;
      setState(() {});
      VideoListResp resp = await VidService.sectionExchange(widget.item?.sectionID);
      if (resp.videos?.isNotEmpty == true) {
        widget.item?.allVideoInfo = resp.videos;
        widget.callback?.call();
      }
    } catch (e) {
      debugLog(e);
    }
    isLoading = false;
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 36,
      margin: widget.margin,
      child: Row(
        children: [
          Expanded(
            child: GestureDetector(
              onTap: () => _loadData(),
              child: Container(
                alignment: Alignment.center,
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(4),
                  color: Color(0x1AFFFFFF),
                ),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    if (isLoading)
                      Container(
                        width: 16,
                        height: 16,
                        alignment: Alignment.center,
                        child: CupertinoActivityIndicator(
                          radius: 8,
                          color: Colors.white,
                        ),
                      )
                    else
                      Image.asset(
                        "section_item_change.webp".homePath,
                        width: 16,
                        height: 16,
                      ),
                    const SizedBox(width: 4),
                    Text(
                      '换一换',
                      textAlign: TextAlign.center,
                      style: TextStyle(
                        color: Color(0xE5FFFFFF),
                        fontSize: 14,
                        fontWeight: FontWeight.w400,
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ),
          16.sizeBoxW,
          Expanded(
            child: GestureDetector(
              onTap: () {
                Get.to(SpecailTopicsDetailPage(widget.item), preventDuplicates: false);
              },
              child: Container(
                alignment: Alignment.center,
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(4),
                  color: Color(0x1AFFFFFF),
                ),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Image.asset(
                      "section_item_more.webp".homePath,
                      width: 16,
                      height: 16,
                    ),
                    const SizedBox(width: 4),
                    Text(
                      '查看更多',
                      textAlign: TextAlign.center,
                      style: TextStyle(
                        color: Color(0xE5FFFFFF),
                        fontSize: 14,
                        fontWeight: FontWeight.w400,
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
